﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.dataAcess;
using dhSystemBLL.basicData;
using dhDataAcess.Model;
using baseSet.importExport;
using dhsystem.Main;
using baseSet.power;

namespace dhsystem.basicForm
{
    public partial class FrmBasList : Form
    {
        basListBLL basList = new basListBLL();
        basListModel basListRow = new basListModel();
        public FrmBasList()
        {
            InitializeComponent();
        }
        //新增按钮，显示新增数据组件
        private void toolAdd_Click(object sender, EventArgs e)
        {
            checkAdd.Visible = true;
            if (lblState.Text == "编辑")
            {
                MessageBox.Show("当前在编辑数据状态，请先保存编辑数据或者取消再进行编辑");
                return;
            }
            lblState.Text = "新增";
            if (groupAdd.Visible == false)
            {
                groupAdd.Visible = true;
                groupList.Width = groupList.Width - 288;
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
                textBasType.Focus();
            }
        }
        //打开窗口时显示列表数据并且隐藏新增数据组件
        private void FrmBasList_Load(object sender, EventArgs e)
        {
            bindingSourceBasList.DataSource = basList.getbasListAll();
            dGridBasList.DataSource = bindingSourceBasList;
            groupList.Width = 288 + groupList.Width;
            groupAdd.Visible = false;
            DcolumnSet.colSet(moduleInfo.basList , dGridBasList );
            toolStripStatusLabel1.Text = "数据总条数:" + dGridBasList.RowCount.ToString() + "条";

            sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.basList );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                //buttImport.Enabled = modulAaccessRow.importAc;

            }
        }
        //取消按钮，隐藏新增数据组件
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            groupList.Width = 288 + groupList.Width;
            groupAdd.Visible = false;
            lblState.Text = "正常";
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (lblState.Text == "新增")
            {

                try
                {
                    DialogResult result = MessageBox.Show("是否新增基础信息数据！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        basListRow.basType = textBasType.Text;
                        basListRow.basNum = textBasNum.Text;
                        basListRow.basNameI = textBasNameI.Text;
                        basListRow.basNameII = textBasNameII.Text;
                        basListRow.isSys = comboBoxisSys.Text;
                        basListRow.rmark = textRmark.Text;
                        basListRow.recordid =Convert .ToInt32 ( textid.Text);
                        basListRow.createDate = DateTime .Now ;
                        basListRow.mark = textmark.Text;
                        basList.basListInsert(basListRow);
                        bindingSourceBasList.DataSource = basList.getbasListAll();
                        if (checkAdd.Checked == true)
                        {
                            lblState.Text = "新增";
                        }
                        else
                        {
                            lblState.Text = "正常";
                        }
                        return;
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }

            if (lblState.Text == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否更改基础信息数据！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        basListRow.basType = textBasType.Text;
                        basListRow.basNum = textBasNum.Text;
                        basListRow.basNameI = textBasNameI.Text;
                        basListRow.basNameII = textBasNameII.Text;
                        basListRow.isSys = comboBoxisSys.Text;
                        basListRow.rmark = textRmark.Text;
                        basListRow.recordid = Convert.ToInt32(textid.Text);
                        basListRow.createDate = DateTime .Now ;
                        basListRow.mark = textmark.Text;
                        basList.basListUpdate (basListRow);
                        bindingSourceBasList.DataSource = basList.getbasListAll();
                        lblState.Text = "正常";
                        return;
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            checkAdd.Visible = false;
            try
            {
                if (lblState.Text == "新增")
                {
                    MessageBox.Show("当前在新增数据状态，请先保存新增数据或者取消再进行编辑");
                    return;
                }

                lblState.Text = "编辑";

                if (groupAdd.Visible == false)
                {
                    groupAdd.Visible = true;
                    groupList.Width = groupList.Width - 288;
                    groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
                }

                DataGridViewRow currentRow = dGridBasList.CurrentRow;
                textBasType.Text = currentRow.Cells["basType"].Value.ToString();
                textBasNum.Text = currentRow.Cells["basNum"].Value.ToString();
                textBasNameI.Text = currentRow.Cells["basNameI"].Value.ToString();
                textBasNameII.Text = currentRow.Cells["basNameII"].Value.ToString();
                comboBoxisSys.Text = currentRow.Cells["isSys"].Value.ToString();
                textRmark.Text = currentRow.Cells["rmark"].Value.ToString();
                textid.Text = currentRow.Cells["recordid"].Value.ToString();
                textmark.Text = currentRow.Cells["mark"].Value.ToString();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string recordid = dGridBasList.Rows[dGridBasList.CurrentRow.Index].Cells[0].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除权限组编号为" + recordid + "的权限组信息！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                  bindingSourceBasList.DataSource=basList .basListDel (recordid);
                  dGridBasList.DataSource = bindingSourceBasList;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            bindingSourceBasList.DataSource = basList.getbasListAll();
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.basList );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridBasList.DataSource = query.rTable;
            }
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSourceBasList .DataSource =basList.getbasListAll("%" + textBasNameIQuery.Text  + "%");
                dGridBasList.DataSource =bindingSourceBasList ;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //工具栏退出按钮
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmBasList_Resize(object sender, EventArgs e)
        {
            if (groupAdd.Visible == true)
            {
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
            }
        }
        //datagridview第一列显示行号
        private void dGridBasList_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1); 
        }

        private void toolPrint_Click(object sender, EventArgs e)
        {
          
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.basList, dGridBasList, "基础信息");
            frm.ShowDialog();
        }
       
    }
}
